package com.groot.medical.service;

import com.groot.medical.dao.FamilyArchivesDao;
import com.groot.medical.dao.FamilyArchivesDaoImpl;
import com.groot.medical.dao.PersonalFileDao;
import com.groot.medical.dao.PersonalFileDaoImpl;
import com.groot.medical.entity.PageBean;
import com.groot.medical.entity.PersonalFiles;

public class PersonalFileServiceImpl implements PersonalFileService {
	private PersonalFileDao personalFileDao = new PersonalFileDaoImpl();
	private FamilyArchivesDao familyArchivesDao = new FamilyArchivesDaoImpl();
	@Override
	public void addHolderPersonInfo(PersonalFiles person,String groupArea) {
		String familyCode=familyArchivesDao.findMaxFamilyCode(groupArea);
		person.setFarmingcardCode(familyCode+person.getFamilyInCode());
		person.setFamilyCode(familyCode);
		int i=personalFileDao.addHolderPersonInfo(person);
		
	}
	@Override
	public PageBean findPersonByPage(int pageNum, int pageSize) {
		int totalRecord = personalFileDao.getAllPersonCount();
		PageBean pageBean = new PageBean<>(pageNum, pageSize, totalRecord);
		int startIndex = pageBean.getStartIndex();
		pageBean.setList(personalFileDao.findPersonByPage(startIndex,pageSize));
		return pageBean;
	}
	@Override
	public void addPersonInfo(PersonalFiles person) {
		//自动生成户内编号
		String familyInCode=personalFileDao.autoCreateFamilyInCode(person.getFamilyCode());
		person.setFamilyInCode(familyInCode);
		person.setFarmingcardCode(person.getFamilyCode()+familyInCode);	
		int i=personalFileDao.addHolderPersonInfo(person);
		if(i!=0){
			//查询该家庭的人口数
			int personCounts = familyArchivesDao.findFamilyByFamilyCode(person.getFamilyCode());
			//修改家庭人口数
			familyArchivesDao.updataPersonCountsByFamilyCode(personCounts,person.getFamilyCode());
		}
	}
	@Override
	public PersonalFiles findPersonInfoById(String idcardNumber) {
		// TODO Auto-generated method stub
		return personalFileDao.findPersonInfoById(idcardNumber);
	}
	@Override
	public void editPersonInfo(PersonalFiles person) {
		personalFileDao.editPersonInfo(person);
		
	}
	@Override
	public void deletePersonInfo(String idCardNum,String familyCode) {
		//查询人口数
		
		int i= personalFileDao.deletePersonInfo(idCardNum);
		if(i>0){
			int max=familyArchivesDao.findFamilyByFamilyCode(familyCode);
			
			familyArchivesDao.updataPersonCountsByFamilyCode(max, familyCode);
		}
		
	}
	

}
